package it.cqupt.service.impl;

import it.cqupt.bean.Student;
import it.cqupt.mapper.StudentMaper;
import it.cqupt.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> selectAll() {
        InputStream is = null;
        SqlSession sqlSession =  null;
        List<Student> students = null;
        try{
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMaper stuMap = sqlSession.getMapper(StudentMaper.class);
            students = stuMap.selectAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return students;
    }

    @Override
    public Student selectById(Integer id) {
        InputStream is = null;
        SqlSession sqlSession =  null;
        Student student = null;
        try{
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMaper stuMap = sqlSession.getMapper(StudentMaper.class);
            student = stuMap.selectById(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return student;
    }

    @Override
    public int insertToStu(Student stu) {
        InputStream is = null;
        SqlSession sqlSession =  null;
        Student student = null;
        int res = 0;
        try{
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMaper stuMap = sqlSession.getMapper(StudentMaper.class);
            res = stuMap.insertToStu(stu);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    @Override
    public int updateAllById(Student stu) {
        InputStream is = null;
        SqlSession sqlSession =  null;
        Student student = null;
        int res = 0;
        try{
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMaper stuMap = sqlSession.getMapper(StudentMaper.class);
            res = stuMap.updateAllById(stu);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    @Override
    public int deleteById(Integer id) {
        InputStream is = null;
        SqlSession sqlSession =  null;
        Student student = null;
        int res = 0;
        try{
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMaper stuMap = sqlSession.getMapper(StudentMaper.class);
            res = stuMap.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

}
